package com.wanmait.schedule.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanmait.schedule.entity.*;
import com.wanmait.schedule.mapper.ProjectMapper;
import com.wanmait.schedule.mapper.TaskMapper;
import com.wanmait.schedule.mapper.UserinfoMapper;
import com.wanmait.schedule.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 任务表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private CompanyUserService companyUserService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private RejectService rejectService;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private DataService dataService;
    @Override
    public void addTask(Task task) {
//        task.setSubTime(LocalDateTime.now());
        this.save(task);
    }

    @Override
    public List<Task> taskList(Integer pubId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getPubId,pubId);
        queryWrapper.eq(Task::getIspass,false);
        queryWrapper.eq(Task::getSubmit,true);
        List<Task> taskList = this.list(queryWrapper);
        if(taskList==null){
            return null;
        }
        taskList.forEach(task -> {
           Integer companyUserId = task.getCompanyUserId();
            CompanyUser companyUser = companyUserService.getById(companyUserId);
           Integer userinfoId = companyUser.getUserinfoId();
           companyUser.setUserinfo(userinfoService.getById(userinfoId));
            task.setCompanyUser(companyUser);
            LambdaQueryWrapper<Data> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(Data::getTaskId,task.getId());
            List<Data>dataList = dataService.list(lambdaQueryWrapper);
            task.setDataList(dataList);

        });
         return taskList ;
    }

    //通过企业用户id查询所有的任务
    @Override
    public List<Task> findAllTask(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }
    //通过企业用户id查询企业直接下发的任务
    @Override
    public List<Task> enterpriseTask(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }

    //通过企业用户id查询出已审核通过的项目
    @Override
    public List<Task> completedFindAll(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,1);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }
    //通过企业用户id查询出已审核的企业直接下发的项目
    @Override
    public List<Task> completedCompanyFindAll(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,1);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }
    //通过企业id查询出已提交的项目
    @Override
    public List<Task> submittedFindAll(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }
    //通过企业用户id查询出已提交的企业直接下发的项目
    @Override
    public List<Task> submittedCompanyFindAll(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }

    //通过企业id和部门id查询所有的任务(未完成)
    @Override
    public List<Task> findAllContinued(Task task1) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyId,task1.getCompanyId());
        queryWrapper.eq(Task::getDepartmentId,task1.getDepartmentId());
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
        });
        return tasks;
    }

    //通过企业id和部门id查询所有的任务(已提交)
    @Override
    public List<Task> findAllSubmitted(Task task1) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyId,task1.getCompanyId());
        queryWrapper.eq(Task::getDepartmentId,task1.getDepartmentId());
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
        });
        return tasks;
    }

    //通过企业id和部门id查询所有的任务(已完成)
    @Override
    public List<Task> findAllDone(Task task1) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyId,task1.getCompanyId());
        queryWrapper.eq(Task::getDepartmentId,task1.getDepartmentId());
        queryWrapper.isNotNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,1);
        queryWrapper.eq(Task::getIspass,1);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
        });
        return tasks;
    }
    //通过企业用户id查询所有的任务(企业直接下发的任务)
    @Override
    public List<Task> findAllCompanyTask(Integer companyUserId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.isNull(Task::getProjectId);
        queryWrapper.eq(Task::getSubmit,0);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
        });
        return tasks;
    }
    //通过企业id和部门id查询所有的任务
    @Override
    public List<Task> findAllQueryAll(Integer companyId, Integer departmentId) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyId,companyId);
        queryWrapper.eq(Task::getDepartmentId,departmentId);
        queryWrapper.isNotNull(Task::getProjectId);
        List<Task> tasks =this.list(queryWrapper);
        tasks.forEach(task -> {
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
            task.setDepartment(departmentService.getById(task.getDepartmentId()));
            task.setProject(projectMapper.selectById(task.getProjectId()));
            task.setCompanyUser(companyUserService.getById(task.getCompanyUserId()));
        });
        return tasks;
    }

    //通过企业id和任务id查询项目
    @Override
    public Task findTaskById(Task task1) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,task1.getCompanyUserId());
        queryWrapper.eq(Task::getId,task1.getId());
        queryWrapper.isNotNull(Task::getProjectId);
        Task task=this.getOne(queryWrapper);
        task.setProject(projectMapper.selectById(task.getProjectId()));
        return task;
    }

    @Override
    public List<CompanyUser> findResignMember(Integer departmentId) {

//        查找这个部门id底下的所有 没审核没提交的任务
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ispass",false).eq("submit",false);
        List<Task>taskList = new ArrayList<>();
        taskList = this.list(queryWrapper);
        HashSet<Integer> set = new HashSet<>();
//        将任务 没通过的人员id进行存储
//        List<Integer> companyUserIds = new ArrayList<>();
        taskList.forEach(task -> {
            Integer companyUserId = task.getCompanyUserId();
            set.add(companyUserId);

        });
        LambdaQueryWrapper<CompanyUser>companyUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyUserLambdaQueryWrapper.eq(CompanyUser::getFlag,true).in(CompanyUser::getId,set);
        List<CompanyUser> companyUserList = companyUserService.list(companyUserLambdaQueryWrapper);
        companyUserList.forEach(companyUser -> {
             companyUser.setUserinfo(userinfoService.getById(companyUser.getUserinfoId()));
             LambdaQueryWrapper<Task> taskLambdaQueryWrapper = new LambdaQueryWrapper<>();
             taskLambdaQueryWrapper.eq(Task::getCompanyUserId,companyUser.getId());
             companyUser.setTaskList(this.list(taskLambdaQueryWrapper));
        });
        return companyUserList;

    }

    @Override
    public void overTask(Integer taskId) {
        Task task = new Task();
        task.setSubmit(true).setIspass(true).setIscheck(true);
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Task::getCompanyUserId,taskId);
        this.update(task,queryWrapper);
    }

    @Override
    public void taskPass(Integer taskId) {
        Task task = new Task();
        task.setCheckTime(LocalDateTime.now());
        task.setIscheck(true);
        task.setIspass(true);
        task.setId(taskId);
        task.setEndTime(LocalDateTime.now());
        this.updateById(task);
    }

    @Override
    public void taskNotPass(Integer taskId, Reject reject) {
        Task task = new Task();
        task.setCheckTime(LocalDateTime.now());
        task.setSubmit(false);
        task.setId(taskId);
        this.updateById(task);
        reject.setRejectTime(LocalDateTime.now());
        rejectService.save(reject);
    }

    @Override
    public void addExtraTask(Task task) {
        this.save(task);
    }

    @Override
    public Page<Task> memberTaskPageList(Integer companyUserId, Integer pageNum, Integer pageSize, String taskName, String taskDetail) {
        List<Task> taskList = new ArrayList<>();
        Page page = new Page(pageNum,pageSize);
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Task::getCompanyUserId,companyUserId);
        queryWrapper.like(!StringUtils.isEmpty(taskDetail),Task::getDetails,taskDetail).like(!StringUtils.isEmpty(taskName),Task::getName,taskName);
        return this.page(page,queryWrapper);
    }

    @Override
    public Task taskDetail(Integer taskId) {
        return this.getById(taskId);
    }

    @Override
    public void saveTask(Task task) {
        this.updateById(task);
    }



}
